﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCodeCreater.Module
{
    public class Tool
    {
        public static DataTable GetTableInfo(string tablename, SqlConnection trainConnection)
        {
            string selectCommandText = "Select sc.name,systypes.name,sc.isnullable,sc.length,sc.prec,(select isnull(f.[value],'') from sys.columns c left join sys.extended_properties f on c.object_id = f.major_id AND f.minor_id = c.column_id where c.object_id=sc.id and c.name=sc.name) as Remark,( SELECT CASE WHEN COLUMN_NAME=sc.name THEN 1 ELSE 0 end FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME='" + tablename + "') isPK FROM SysColumns sc inner join systypes on sc.xusertype=systypes.xusertype Where id=Object_Id('" + tablename + "')";
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommandText, trainConnection);
            DataSet dataSet = new DataSet();
            sqlDataAdapter.Fill(dataSet, "ds1");
            return dataSet.Tables[0];
        }
        public static string GetCsharpTypeInSql(string sqltype,string isnullStr,out Type strType)
        {
            string text2 = "string";
            strType = typeof(string);
            switch (sqltype)
            {
                case "date":
                    text2 = "DateTime";
                    strType = typeof(DateTime);
                    break;
                case "time":
                    text2 = "DateTime";
                    strType = typeof(DateTime);
                    break;
                case "datetime2":
                    text2 = "DateTime";
                    strType = typeof(DateTime);
                    break;
                case "datetimeoffset":
                    text2 = "DateTime";
                    strType = typeof(DateTime);
                    break;
                case "tinyint":
                    text2 = "int";
                    strType = typeof(int);
                    break;
                case "smallint":
                    text2 = "short";
                    strType = typeof(int);
                    break;
                case "int":
                    text2 = "int";
                    strType = typeof(int);
                    break;
                case "smalldatetime":
                    text2 = "DateTime";
                    strType = typeof(DateTime);
                    break;
                case "money":
                    text2 = "decimal";
                    strType = typeof(decimal);
                    break;
                case "datetime":
                    text2 = "DateTime";
                    strType = typeof(DateTime);
                    break;
                case "float":
                    text2 = "float";
                    strType = typeof(float);
                    break;
                case "bit":
                    text2 = "bool";
                    strType = typeof(bool);
                    break;
                case "decimal":
                    text2 = "decimal";
                    strType = typeof(decimal);
                    break;
                case "numeric":
                    text2 = "int";
                    strType = typeof(int);
                    break;
                case "smallmoney":
                    text2 = "double";
                    strType = typeof(double);
                    break;
                case "bigint":
                    text2 = "long";
                    strType = typeof(long);
                    break;
                case "timestamp":
                    text2 = "int";
                    strType = typeof(int);
                    break;
                case "uniqueidentifier":
                    text2 = "Guid";
                    strType = typeof(Guid);
                    break;
            }
            if (text2 != "string" && isnullStr == "1")
            {
                text2 += "?";
                strType = null;
            }
            return text2;
        }


        public static object DefaultForType(Type targetType)
        {
            return targetType.IsValueType ? Activator.CreateInstance(targetType) : null;
        }

        public static void SaveFile(string path, string content)
        {
            var dir = path.Substring(0, path.LastIndexOf("\\"));
            Directory.CreateDirectory(dir);
            File.WriteAllText(path, content);
        }
    }
}
